var app = getApp() 
Page({
  data: {
    userInfo: {
      nickname: '',
      phone: '',
      sex: '0',
      cover_thumb: 'http://img.zhichiwangluo.com/zc_app_default_photo.png',
      birthdate: '',
      address:'',
    },
    genderArr: [{key:'0',value:'女'},{key:'1',value:'男'}], 
  },
  loadLoginUser:function(){ 
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    var _this = this;
    if(loginUserId == ''){
      wx.redirectTo({
        url: '../index/index?redirect=my-center/my-center'
      })
    }else{   
      wx.request({ 
        url: app.globalData.user_detail_url,
        data: {userId:loginUserId},
        method: 'POST', 
        success: function (res) {
          var resultData = res.data;
          if(resultData.errno == 0){
            _this.setData({userInfo: resultData.data}) 
          }
        },
        fail: function () {
          setTimeout(function () {
            wx.showToast({
              title: "加载失败"
            })
          }, 100)
        }
      })
    } 
  },
  onShow: function () {
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    var _this = this;
    if(loginUserId == ''){
      wx.redirectTo({
        url: '../index/index?redirect=my-center/my-center'
      })
    }
  },
  onLoad: function(){ 
    this.loadLoginUser();
  },
  choosePhoto: function(){
    var that = this;
    wx.chooseImage(function(imgUrl){
      that.setData({
        'userInfo.cover_thumb': imgUrl
      })
    });
  },
  changeGender: function(e){
    this.setData({
      'userInfo.sex': e.detail.value
    })
  },
  inputNickname: function(e){
    this.setData({
      'userInfo.nickname': e.detail.value
    })
  },
  inputPhone: function(e){
    this.setData({
      'userInfo.phone': e.detail.value
    })
  },
  inputAddress: function(e){
    this.setData({
      'userInfo.address': e.detail.value
    })
  },
  saveUserInfo: function(){
    var data = this.data.userInfo;
    wx.request({ 
      url: app.globalData.user_update_url,
      method: 'POST',
      data: data,
      success: function(res){
        var resultData = res.data;
        if(resultData.errno == 0){
         app.showSuccess(resultData.message)
        }
      }
    });
  }, 
  radioChange: function (e) { 
    this.setData({
      'userInfo.sex': e.currentTarget.dataset.key
    }) 
  }, 
  bindDateChange: function(e) { 
    this.setData({
      'userInfo.birthdate': e.detail.value
    })
  },

})




